
package myDB;

import java.sql.*;

public class myDB {
    Connection con = null;

    public void setupConnection(){
        try{
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            System.out.println("Loanding Driver successful");
        }catch(ClassNotFoundException ex){
            System.out.println("Loading driver Unsuccessful");
        }

        try{
            String Url = "jdbc:sqlserver://127.0.0.1:1031;databaseName=myBank";
            // Url = "jdbc:sqlserver://<127.0.0.1>:<TCP Dynamic port>;databaseName=myBank";
            Connection cn = null;
            cn = DriverManager.getConnection(Url,"sa","123");
            System.out.println("Connection succsessful");
        }catch(SQLException ex){
            System.out.println("Connection Unsuccessful");
        }
    }

    protected void finalize() throws Throwable{
        try{
            if(con!=null)
            con.close();
            con = null;
        }catch(SQLException ex){
            System.out.println("Can not close Connection");
        }
    }

    public void viewRecords(){
        ResultSet rs = null;
        Statement sa = null;
        try{
            sa = con.createStatement();
            rs = sa.executeQuery("select * from myCustomer");
            while(rs.next())
            {
                System.out.println(rs.getString(1) +"\t"+ rs.getString(2) +"\t"+ rs.getString(3));
            }
        }catch(SQLException ex){
            System.out.println(ex);
        }
        finally{
            try{
                rs.close();
                sa.close();
            }catch(Exception ex){
            }
        }
    }
    
    public myDB(){
        this.setupConnection();
    }
    public static void main(String args[]){
        new myDB();
    }
}
